package com.ruoyi.web.controller.api.suggest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.bo.PAboutBo;
import com.ruoyi.system.domain.bo.PServerBo;
import com.ruoyi.system.domain.bo.PSuggestBo;
import com.ruoyi.system.domain.vo.PAboutVo;
import com.ruoyi.system.domain.vo.PServerVo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.IPAboutService;
import com.ruoyi.system.service.IPServerService;
import com.ruoyi.system.service.IPSuggestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/7/13 8:46
 * @Version: 1.0
 */
@Validated
@Api(value = "意见反馈控制器", tags = {"前端-信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/message")
public class SuggestApiController extends BaseController {
    private final IPSuggestService iPSuggestService;
    private final IPAboutService iPAboutService;
    private final IPServerService iPServerService;
    private final SysDictDataMapper sysDictDataMapper;

    /**
     * 新增意见反馈
     */
    @ApiOperation("新增意见反馈")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PSuggestBo bo) {
        bo.setUserId(getLoginUser().getUserId());
        return toAjax(iPSuggestService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 删除意见反馈
     */
    @ApiOperation("删除意见反馈")
    @GetMapping("/del/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPSuggestService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("使用帮助分页")
    @GetMapping("/get-help")
    public TableDataInfo<PAboutVo> getHelp(PAboutBo bo, PageQuery pageQuery) {
        String type = bo.getType();
        if (StringUtils.isBlank(type)) {
            throw new ServiceException("文本类型不能为空!");
        }

        return iPAboutService.queryPageList(bo, pageQuery);
    }

    @ApiOperation("0 关于我们/1 使用帮助")
    @GetMapping("/get-about")
    public R<List<PAboutVo>> getAbout(PAboutBo pAboutBo) {
        String type = pAboutBo.getType();
        if (StringUtils.isBlank(type)) {
            return R.fail("文本类型不能为空！");
        }

        return R.ok(iPAboutService.queryList(pAboutBo));
    }

    /**
     * 查询联系客服列表
     */
    @ApiOperation("查询联系客服列表")
    @GetMapping("/serve-list")
    public TableDataInfo<PServerVo> list(PServerBo bo, PageQuery pageQuery) {
        return iPServerService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取联系客服详细信息
     */
    @ApiOperation("获取联系客服详细信息")
    @GetMapping("/del-serve/{id}")
    public R<PServerVo> getInfo(@ApiParam("主键")
                                @NotNull(message = "主键不能为空")
                                @PathVariable("id") Long id) {
        return R.ok(iPServerService.queryById(id));
    }

    /**
     * 新增联系客服
     */
    @ApiOperation("新增联系客服")
    @RepeatSubmit()
    @PostMapping("/add-server")
    public R<Void> addServe(@Validated(AddGroup.class) @RequestBody PServerBo bo) {
        return toAjax(iPServerService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 删除联系客服
     */
    @ApiOperation("删除联系客服")
    @PostMapping("/del-serve/{ids}")
    public R<Void> removeServe(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPServerService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("货物状态")
    @GetMapping("/goodsStatus")
    public R<List<SysDictData>> goodsStatus() {
        return R.ok(this.sysDictDataMapper.selectList(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictType, "goods_status")));
    }

    @ApiOperation("退款原因")
    @GetMapping("/refundReason")
    public R<List<SysDictData>> refundReason() {
        return R.ok(this.sysDictDataMapper.selectList(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictType, "refund_reason")));
    }
}
